// pages/welfare_bar/welfare_bar.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showit: true,
    list: [],
    showsign: false,
    swiperCurrent2: 0,
    showbanner: true,
    indicatorColor: '#EAEAEA',
    indicatorActiveColor: '#ffffff',
    indicatorDots2: false,
    authbg: getApp().globalData.authImg,
    authtitle: getApp().globalData.source + "red_packet/title1.png",
    authbtn2: getApp().globalData.source + "red_packet/btn2.png",
    authdialog: getApp().globalData.source + "dialog/dialog.png",
    imgUrls: []
  },
  //轮播图的切换事件 
  swiperChange2: function(e) {
    console.log(e);
    this.setData({
      swiperCurrent2: e.detail.current //获取当前轮播图片的下标
    })
  },
  bindGetUserInfo: function(e) {
    getApp().authInfo(e);
  },
  focusPackets: function() {
    wx.navigateTo({
      url: '../focus_packets/focus_packets',
    })
  },
  tonext: function(e) {
    var type = e.currentTarget.dataset.type;
    var title = e.currentTarget.dataset.title;
    var redirectUri = e.currentTarget.dataset.redirecturi;
    if (redirectUri == "" || redirectUri == undefined) {
      getApp().showMsgToast(title)
      return
    }
    if (type == "0") {
      wx.navigateTo({
        url: redirectUri,
      })
    } else {
      wx.navigateTo({
        url: "../webview/webview?url=" + redirectUri,
        success: function() {},
        fail: function() {}
      })
    }
  },
  getbanner2: function() {
    var that = this;
    //加载首页banner数据
    var data = {
      "type": 10,
      "platform": wx.getStorageSync("platform")
    }
    getApp().requestLoading(getApp().globalData.https + "programResource/commonBannerList",
      "get", data, "加载中...",
      function(res) {
        if (res.code == 0) {
          // 获取系统信息 
          var data = res.data;
          var iphone = wx.getStorageSync("iPhone");
          if (iphone != "-1") {
            that.setData({
              showbanner: false
            })
          } else {
            that.setData({
              imgUrls2: data,
            });
          }
        }
      },
      function(res) {
        console.log(res)
      }
    )
  },
  tobannerdetails: function(e) {
    var vm = this
    var index = e.currentTarget.dataset.index;
    var bn = vm.data.imgUrls2[index]
    //统计数据埋点
    vm.setData({
      userIdss: wx.getStorageSync("userId"), //用户编号
      homeBannerValue: bn.cSrc, //首页banner 栏目id，如果是H5就是H5连接地址，如果是栏目就是Id记录 
    })
    if (bn.isH5 == 1) {
      //跳转原生详情页 
      wx.navigateTo({
        url: "../day_day_listen/details/details?id=" + bn.cSrc + "&season=" + 1,
        success: function() {},
        fail: function() {}
      })
    } else if (bn.isH5 == 0) {
      //跳转h5详情页
      wx.navigateTo({
        url: "../webview/webview?url=" + encodeURIComponent(bn.cSrc),
        success: function() {},
        fail: function() {}
      })
    } else {
      if ("/pages/welfare_bar/welfare_bar" == bn.cSrc) {
        this.onShow()
      } else {
        wx.navigateTo({
          url: bn.cSrc,
          success: function() {},
          fail: function() {}
        })
      }
    }
  },

  todetails: function() {
    wx.navigateTo({
      url: '/pages/my/mydetail/mydetail',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getdata()
    this.getbanner2()
    var iphone = wx.getStorageSync("iPhone");
    if (iphone == "1") {
      this.setData({
        showit: false
      })
    } else {
      this.setData({
        showit: true
      })
    }
  },

  authbtn: function() {
    var vm = this
    wx.showLoading({
      title: "正在检查网络.."
    })
    this.setData({
      authbtn: true
    })
  },
  getdata: function() {
    var vm = this

    var data;
    if (wx.getStorageSync("platform") != "0") {
      data = {}
    } else {
      data = {
        "platform": wx.getStorageSync("platform"),
      }
    }
    getApp().request(getApp().globalData.https + "trueOrFalseResource/welfareItem",
      "get", data,
      function(res) {
        if (res.code == 0) {
          if (res.data.length <= 4) {
            vm.setData({
              list: res.data,
              showloading: false
            })

            if (res.data.length == 0) {
              vm.setData({
                list: [],
                list2: [],
                showloading: true
              })
            }
          } else {
            var bottomdata = [];
            var topdata = [];

            for (var i = 0; i < res.data.length; i++) {
              if (i <= 3) {
                topdata.push(res.data[i])
              } else {
                bottomdata.push(res.data[i])
              }
            }

            vm.setData({
              list: topdata,
              list2: bottomdata,
              showloading: false
            })
          }

        }
      },
      function(res) {});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  signdetials: function() {
    wx.navigateTo({
      url: '/pages/sign_on/sign_on',
    })
  },
  //判断是否签到
  checkIsSignOn: function() {
    var iphone = wx.getStorageSync("iPhone");
    if (iphone != "-1") {
      return
    }
    var vm = this
    var data = {
      userId: wx.getStorageSync("userId")
    };
    getApp().request(getApp().globalData.https + "activityResource/currentDaySignIn",
      "get", data,
      function(res) {
        if (res.code == 0) {
          if (res.data.currentDaySignIn == "0") {
            vm.setData({
              showsign: true
            })
          } else {
            vm.setData({
              showsign: false
            })
          }

        }
      },
      function(res) {}
    )
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getuserinfo()
    this.checkIsSignOn();
    if (wx.getStorageSync("userId") == "" || wx.getStorageSync("userId") == undefined) {
      if (wx.getStorageSync("tourist") == "1") {
        if (wx.getStorageSync("authtype") == "0") {
          this.setData({
            showAuth: true,
            showAuth2: false
          })
        } else {
          this.setData({
            showAuth: false,
            showAuth2: true
          })
        }
        return
      }
    } else {
      this.setData({
        showAuth: false,
        showAuth2: false
      })
    }

  },

  //获取关注天天必听公账号h5地址
  getuserinfo: function() {
    var vm = this
    var data = {
      userId: wx.getStorageSync("userId")
    }
    getApp().requestLoading(getApp().globalData.https + "userResource/userDetailInfo",
      "get", data, "加载中..",
      function(res) {
        if (res.code == 0) {
          vm.setData({
            infodata: res.data
          })
        }
      },
      function(res) {});
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})